home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* Routines to manipulate 3 dimensional vectors. All these routines
- * should work even if the input and output vectors are the same.
- *
- * -- Tom Davis 1990
- *
- */
-
- #include <stdio.h>
- #include <math.h>
- #include "3d.h"
-
- void (*errfunc)(char *) = 0;
-
- void seterrorfunc(void (*func)(char *))
- {
- errfunc = func;
- }
-
- void error(char *s)
- {
- if (errfunc)
- (*errfunc)(s);
- else {
- fprintf(stderr, s);
- fprintf(stderr, "\n");
- exit(1);
- }
- }
-
- void diff3(float p[3], float q[3], float diff[3])
- {
- diff[0] = p[0] - q[0];
- diff[1] = p[1] - q[1];
- diff[2] = p[2] - q[2];
- }
-
- void add3(float p[3], float q[3], float sum[3])
- {
- sum[0] = p[0] + q[0];
- sum[1] = p[1] + q[1];
- sum[2] = p[2] + q[2];
- }
-
- void scalarmult(float s, float v[3], float vout[3])
- {
- vout[0] = v[0]*s;
- vout[1] = v[1]*s;
- vout[2] = v[2]*s;
- }
-
- float dot3(float p[3], float q[3])
- {
- return p[0]*q[0] + p[1]*q[1] + p[2]*q[2];
- }
-
- float length3(float v[3])
- {
- return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
- }
-
- float dist3(float p[3], float q[3])
- {
- float d[3];
-
- diff3(p, q, d);
- return length3(d);
- }
-
- void copy3(float old[3], float new[3])
- {
- new[0] = old[0], new[1] = old[1], new[2] = old[2];
- }
-
- void crossprod(float v1[3], float v2[3], float prod[3])
- {
- float p[3]; /* in case prod == v1 or v2 */
-
- p[0] = v1[1]*v2[2] - v2[1]*v1[2];
- p[1] = v1[2]*v2[0] - v2[2]*v1[0];
- p[2] = v1[0]*v2[1] - v2[0]*v1[1];
- prod[0] = p[0]; prod[1] = p[1]; prod[2] = p[2];
- }
-
- void normalize(float v[3])
- {
- float d;
-
- d = sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
- if (d == 0.0) {
- error("normalize: zero length vector");
- v[0] = d = 1.0;
- }
- d = 1/d;
- v[0] *= d; v[1] *= d; v[2] *= d;
- }
-
- void print3(float v[3])
- {
- float len;
-
- len = length3(v);
- printf("(%g %g %g); len: %g\n", v[0], v[1], v[2], len);
- }
-
- void printmat3(float m[3][3])
- {
- int i, j;
-
- for (i=0; i<3; i++) {
- for (j=0; j<3; j++)
- printf("%7.4f ", m[i][j]);
- printf("\n");
- }
- }
-
- void identifymat3(float m[3][3])
- {
- int i, j;
-
- for (i=0; i<3; i++)
- for (j=0; j<3; j++)
- m[i][j] = (i == j) ? 1.0 : 0.0;
- }
-
- void copymat3(float *to, float *from)
- {
- int i;
-
- for (i=0; i<9; i++) {
- *to++ = *from++;
- }
- }
-
- void xformvec3(float v[3], float m[3][3], float vm[3])
- {
- float result[3]; /* in case v == vm */
- int i;
-
- for (i=0; i<3; i++) {
- result[i] = v[0]*m[0][i] + v[1]*m[1][i] + v[2]*m[2][i];
- }
- for (i=0; i<3; i++) {
- vm[i] = result[i];
- }
- }
-
- long samepoint(float p1[3], float p2[3])
- {
- if (p1[0] == p2[0] && p1[1] == p2[1] && p1[2] == p2[2])
- return 1;
- return 0;
- }
-
- void perpnorm(float p1[3], float p2[3], float p3[3], float n[3])
- {
- float d1[3], d2[3];
-
- diff3(p2, p1, d1);
- diff3(p2, p3, d2);
- crossprod(d1, d2, n);
- normalize(n);
- }
-
-